package hamster.csustef.acquisition.flink.transfer;

import hamster.csustef.acquisition.api.ElectricApi;
import hamster.csustef.common.util.DateUtil;
import org.apache.flink.api.common.functions.MapFunction;


/**
 * 执行数据爬取操作，通过寝室键来获取电费以及爬取时间
 * <p>
 * 输入：寝室键 = 127:A113
 * 输出：(寝室键, 电费, 时间键) = (127:A113, 132.23, 20220614105312)
 *
 * @author yin hamaster
 * @date 2022-06-14
 */
public class GetFeeMapFunction implements MapFunction<String, String> {
    private String seperator = ",";
    private int retryTime = 3;

    @Override
    public String map(String dormKey) throws Exception {
        String fee = ElectricApi.getFee(dormKey);
        int retry = 0;
        while (fee.startsWith("ERROR") && retry < retryTime) {
            fee = ElectricApi.getFee(dormKey);
            retry++;
        }
        return dormKey + seperator + fee + seperator + DateUtil.getNowDateTime();

    }
}
